package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;

import com.moleq.models.BottleSetting;
import com.moleq.models.Departmant;
import com.moleq.models.PosLineItem;
import com.moleq.models.PosLineTares;
import com.moleq.models.TaxSetting;

public class PosLineTaresService {
	private DatabaseHelper DBHelper;
	private MessageDialog mMessageDialog;
	Context context;

	public PosLineTaresService() {

	}

	public PosLineTaresService(Context context) {
		this.context = context;
		this.DBHelper = new DatabaseHelper(context);
	}
	
	public List<PosLineTares> FindAll(){
		List<PosLineTares> obj = new ArrayList<PosLineTares>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from t_pos_line_tares",null);
		if (cursor == null) {
//			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
//			DBError.error = "";
			return obj;
		}
		
		while(cursor.moveToNext()){
			
			int li_id  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosLineTares.LT_ID));
			int lt_line_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosLineTares.LT_LINE_NO));
			int lt_tare_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosLineTares.LT_TARE_NO));
			int lt_qty   = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosLineTares.LT_QTY));
			
			obj.add(new PosLineTares(li_id,lt_line_no,lt_tare_no,lt_qty));
		}
		cursor.close();
		return obj;
	}
	
	public  List<PosLineTares> Find(long no){
		
		List<PosLineTares> obj = new ArrayList<PosLineTares>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = db.rawQuery("select * from t_pos_line_tares where lt_line_no = "+no,null);
		if (cursor == null) {
//			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
//			DBError.error = "";
			return obj;
		}
		while(cursor.moveToNext()){
			
			int li_id  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosLineTares.LT_ID));
			int lt_line_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosLineTares.LT_LINE_NO));
			int lt_tare_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosLineTares.LT_TARE_NO));
			int lt_qty   = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosLineTares.LT_QTY));
			
			obj.add(new PosLineTares(li_id,lt_line_no,lt_tare_no,lt_qty));
		}
		cursor.close();
		return obj;
	
	}
	public void Insert(PosLineTares obj){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"INSERT INTO " + DatabaseSchema.PosLineTares.SCHEMA_NAME + " VALUES(?,?,?,?)",
				new Object[] { null, obj.getLt_line_no(), obj.getLt_tare_no(), obj.getLt_qty() });
	}
	
	public void DeleteAndUpdateNo(long no) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.PosLineTares.SCHEMA_NAME
				+" WHERE "+ DatabaseSchema.PosLineTares.LT_LINE_NO +" <= ?",
				new Object[] { no });
		db.execSQL("Update " + DatabaseSchema.PosLineTares.SCHEMA_NAME + " set lt_line_no = lt_line_no - "+ no);
	}
	
	public void DeleteByNo(long no) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.PosLineTares.SCHEMA_NAME
				+" WHERE "+ DatabaseSchema.PosLineTares.LT_LINE_NO +" = ?",
				new Object[] { no });
	}
	
	public void DeleteByAll() {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.PosLineTares.SCHEMA_NAME);
	}
	
	public long getMaxNo(){
		long no = 0;
		SQLiteDatabase db = DBHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select max(lt_id) from " + DatabaseSchema.PosLineTares.SCHEMA_NAME, null);
		if (cursor.moveToNext()) {
			no = cursor.getLong(0);
		}
		cursor.close();
		return no;
	}
	
	public double getTotalTare(long no){
		double totaltare = 0;
		SQLiteDatabase db = DBHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select sum(lt_qty * ta_value) from t_pos_line_tares,t_tare_setting where ta_no = lt_tare_no and lt_line_no = " + no, null);
		if (cursor.moveToNext()) {
			totaltare = cursor.getDouble(0);
		}
		cursor.close();
		return totaltare;
	}
	
	public int getManualTare(long no){
		int ManualTare = 0;
		SQLiteDatabase db = DBHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select lt_qty  from t_pos_line_tares where lt_tare_no  = 0 and lt_line_no = " + no, null);
		if (cursor.moveToNext()) {
			ManualTare = cursor.getInt(0);
		}
		cursor.close();
		return ManualTare;
	}
	
	public boolean CheckHaveTare(long line_no,int tare_no){
		boolean bo = true;
		SQLiteDatabase db = DBHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select lt_id from t_pos_line_tares where lt_line_no = " +line_no +" and lt_tare_no = " + tare_no, null);
		if (cursor.moveToNext()) {
			bo = false;
		}
		cursor.close();
		return bo;
	}
	public void ShowMessage(String text, int type, int icon) {
		mMessageDialog = new MessageDialog(context);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {

		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onClickOK() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}
	};
}
